//
//  LeftMassageOperation.m
//  massage
//
//  Created by 李志权 on 2017/1/6.
//  Copyright © 2017年 李志权. All rights reserved.
//

#import "LeftMassageOperation.h"
#import "CKCircleView.h"
#import "HorizontalScroller.h"

@interface LeftMassageOperation ()<UIPickerViewDelegate,HorizontalScrollerDelegate>
{
    NSArray  *statesarray;
    
    NSMutableArray *scrollers;
}
/**渐变VIEW*/
@property (weak, nonatomic) IBOutlet UIView *gradientView;
/**进度条*/
@property (nonatomic,strong)CKCircleView *dialView;
@property (weak, nonatomic) IBOutlet UIView *timeView;
/**滚动视图*/
@property (weak, nonatomic) IBOutlet UIView *scrollView;
@end

@implementation LeftMassageOperation

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [Utils navigationBarReturn:self color:[UIColor clearColor] font:fontSizeTitleBold foregroundColor:[UIColor whiteColor]];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = YES;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"登录_left-arrow"] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemClick)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    UIView *backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Width, 364)];
    UIColor *colorOne = [UIColor colorWithRed:(49/255.0)  green:(255/255.0)  blue:(132/255.0)  alpha:1];
    UIColor *colorTwo = [UIColor colorWithRed:(2/255.0)  green:(200/255.0)  blue:(250/255.0)  alpha:1];
    
    CAGradientLayer *gradientLayer =  [CAGradientLayer layer];
    gradientLayer.frame = CGRectMake(0, 0, Width, 364);
    //设置颜色
    [gradientLayer setColors:[NSArray arrayWithObjects:(id)[colorOne CGColor],(id)[colorTwo CGColor], nil]];
    //每种颜色最亮的位置
    //    [gradientLayer setLocations:@[@.5,@.5]];
    //渐变的方向StartPoint－>EndPoint
    [gradientLayer setStartPoint:CGPointMake(0, 0)];
    [gradientLayer setEndPoint:CGPointMake(1, 1)];
    [backgroundView.layer addSublayer:gradientLayer];
    
    self.gradientView.backgroundColor = [UIColor colorWithPatternImage:[self getImageFromView:backgroundView]];
    
    self.gradientView.backgroundColor = [UIColor colorWithPatternImage:[self getImageFromView:backgroundView]];
    
    self.dialView = [[CKCircleView alloc] initWithFrame:CGRectMake(3,9, 260, 245)];
    self.dialView.backColor = [UIColor clearColor];
    self.dialView.dialColor = [UIColor whiteColor];//点颜色
    self.dialView.arcRadius = 255;
    self.dialView.dialRadius = 15.0;//点大小*2
    self.dialView.arcThickness = 3.0;
    [self.timeView addSubview: self.dialView];
    
    UIImage *leftArrArrowImage = [UIImage imageNamed:@"按摩选项设置_icon1"];
    UIImageView *leftArrArrow = [[UIImageView alloc]initWithFrame:CGRectMake(20, backgroundView.YHighly+40, leftArrArrowImage.size.width/2, leftArrArrowImage.size.height/2)];
    leftArrArrow.image = leftArrArrowImage;
    [self.scrollView addSubview:leftArrArrow];
    
    UIImage *rightArrArrowImage = [UIImage imageNamed:@"按摩选项设置_icon2"];
    UIImageView *rightArrArrow = [[UIImageView alloc]initWithFrame:CGRectMake(Width-20-rightArrArrowImage.size.width/2, leftArrArrow.Y, rightArrArrowImage.size.width/2, rightArrArrowImage.size.height/2)];
    rightArrArrow.image = rightArrArrowImage;
    [self.scrollView addSubview:rightArrArrow];
    
    scrollers = [NSMutableArray array];
    
    HorizontalScroller *scroller = [[HorizontalScroller alloc] initWithFrame:CGRectMake(leftArrArrow.XWidth+10, rightArrArrow.Y-12.5,Width-leftArrArrow.XWidth-20-rightArrArrow.width-20, 50)];
    scroller.delegate = self;
    [self.scrollView addSubview:scroller];
  
    
    [scroller setClickArea:CGPointMake(74*2, 40)];
    
    //确定按钮
    UIButton *determine = [UIButton buttonWithType:UIButtonTypeCustom];
    determine.frame = CGRectMake(10, scroller.YHighly+30, Width-20, 40);
    determine.backgroundColor = GreenColor;
    [determine setTitle:@"确定" forState:UIControlStateNormal];
    [determine setShearRound:10 masksToBounds:YES borderWidth:0 borderColor:nil];
    determine.titleLabel.font = fontSizeNormal;
    [determine addTarget:self action:@selector(determineClick) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:determine];
    
    //取消按钮
    UIButton *cancel = [UIButton buttonWithType:UIButtonTypeCustom];
    cancel.frame = CGRectMake(10, determine.YHighly+20, Width-20, 40);
    [cancel setTitle:@"取消" forState:UIControlStateNormal];
    [cancel setTitleColor:GreenColor forState:UIControlStateNormal];
    [cancel setShearRound:10 masksToBounds:YES borderWidth:0.5 borderColor:GreenColor];
    cancel.titleLabel.font = fontSizeNormal;
    [cancel addTarget:self action:@selector(dcancelClick) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:cancel];
   // show from our table view (pops up in the middle of the table)

    
    // Do any additional setup after loading the view from its nib.
}
- (void)leftBarButtonItemClick
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(UIImage *)getImageFromView:(UIView *)theView
{
    // 下面方法，第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果，需要传NO，否则传YES。第三个参数就是屏幕密度了，关键就是第三个参数。\
    
    CGSize size =theView.bounds.size;
    UIGraphicsBeginImageContextWithOptions(size, YES,  [UIScreen mainScreen].scale);
    [theView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - HorizontalScroller Delegate
// 返回滚动视图所有子视图数量的协议方法。因为滚动视图要显示所有专辑的封面，这个数量就是专辑记录的数量。
- (NSInteger)numberOfViewsForHorizontalScroller:(HorizontalScroller*)scroller
{
    return 5;
}

// 指定索引位置的视图

- (UIView *)horizontalScroller:(HorizontalScroller*)scroller viewAtIndex:(int)index
{
    
    
    cellView *albumView =[[cellView alloc] initWithFrame:CGRectMake(0, 0, 75, 50)];
    albumView.label.text = [NSString stringWithFormat:@"%d档",index+1];
    [scrollers addObject:albumView];
    
    return albumView;
}

// 它设置保存当前专辑数据的变量，然后调用showDataForAlbumAtIndex:方法显示专辑数据。
- (void)horizontalScroller:(HorizontalScroller*)scroller clickedViewAtIndex:(int)index
{
    cellView *albumView = scrollers[index];
    
    for (cellView *album in scrollers) {
        if (albumView!=album) {
            [UIView animateWithDuration:0.25 animations:^{
                album.transform = CGAffineTransformMakeScale(0.7, 0.7);
                album.alpha = 0.7;
                album.imageView.image = [UIImage imageNamed:@"按摩选项设置_icon4"];
            }];

        }
        else
        {
            [UIView animateWithDuration:0.25 animations:^{
                album.transform = CGAffineTransformMakeScale(1, 1);
                album.alpha = 1;
                album.imageView.image = [UIImage imageNamed:@"按摩选项设置_icon3"];
            }];

        }
    }

}

// HorizontalScroller的第一个视图设置为了currentAlbumIndex指定的视图。这使得app在下次使用的时候还保留了上次使用的状态。
- (NSInteger)initialViewIndexForHorizontalScroller:(HorizontalScroller *)scroller
{
    return 1;
}
//确定按钮
- (void)determineClick
{
    
}
//取消
- (void)dcancelClick
{
    
}
@end

@implementation cellView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.width/2-10, 5, 20, 20)];
        self.imageView.image = [UIImage imageNamed:@"按摩选项设置_icon4"];
        [self addSubview:self.imageView];
        
        self.label = [[UILabel alloc]initWithFrame:CGRectMake(0, self.imageView.YHighly, self.width, 30)];
        self.label.textColor = GreenColor;
        self.label.textAlignment = NSTextAlignmentCenter;
        self.label.font = [UIFont systemFontOfSize:15];
        [self addSubview:self.label];
        
    }
    return self;
}

@end
